package com.uam.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uam.common.annotation.Log;
import com.uam.db.model.entity.SysLogininfor;
import com.uam.service.ISysLogininforService;
import com.uam.utils.StringUtils;
import com.uam.web.common.model.BusinessType;
import com.uam.web.common.model.PageData;
import com.uam.web.common.model.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * 系统访问记录
 *
 * @author uam
 */
@Controller
@RequestMapping("/monitor/logininfor")
@Api(tags = "登录日志管理")
public class SysLogininforController extends BaseController
{
    private String prefix = "monitor/logininfor";

    @Autowired
    private ISysLogininforService logininforService;

    @ApiOperation(value = "查看登录日志")
    @Log(title = "查看登录日志", businessType = BusinessType.QUERY_LOG)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token" , value = "用户登录token", paramType = "header"),
            @ApiImplicitParam(name = "queryLog" , value = "查询是否生成日志，1：生成", paramType = "header"),
            @ApiImplicitParam(name = "pageNum" , value = "当前页"),
            @ApiImplicitParam(name = "pageSize" , value = "分页条数"),
            @ApiImplicitParam(name = "loginName" , value = "登录名"),
            @ApiImplicitParam(name = "beginTime" , value = "查询开始时间，例：2023-11-01 00:00:00", dataTypeClass = String.class),
            @ApiImplicitParam(name = "endTime" , value = "查询结束时间，例：2023-11-01 23:59:59", dataTypeClass = String.class),
    })
    @PostMapping("/list")
    @ResponseBody
    public Result<PageData<SysLogininfor>> list(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "20") Integer pageSize,
            @RequestParam(required = false) String loginName, @RequestParam(required = false) String beginTime,
            @RequestParam(required = false) String endTime) {
        SysLogininfor logininfor = new SysLogininfor();
        logininfor.setLoginName(loginName);
        HashMap params = new HashMap();
        logininfor.setParams(params);
        if (!StringUtils.isEmpty(beginTime)) {
            params.put("beginTime", beginTime);
        }
        if (!StringUtils.isEmpty(endTime)) {
            params.put("endTime", endTime);
        }
        Page<SysLogininfor> page = new Page(pageNum, pageSize);
        IPage<SysLogininfor> pageResult = logininforService.selectLogininforList(page, logininfor);
        return okPageData(pageResult);
    }
}
